# -*- coding: utf-8 -*-

"""
Module implementing Dialog.
"""

from PyQt5.QtCore import pyqtSlot
from PyQt5.QtGui import QColor
from PyQt5.QtWidgets import QDialog, QListWidgetItem
from log import logger

from .Ui_handlesip import Ui_Dialog

from project import IpState



class Dialog(QDialog, Ui_Dialog):
    """
    Class documentation goes here.
    """
    def __init__(self, parent=None):
        """
        Constructor
        
        @param parent reference to the parent widget
        @type QWidget
        """
        super(Dialog, self).__init__(parent)
        self.setupUi(self)
        self.init()
    
    def init(self):
        self.line_ip.setText("192.168.10.100")

    # 0k
    @pyqtSlot()
    def on_btn_preip_clicked(self):
        """
        Slot documentation goes here.
        """
        text = self.line_ip.text()
        try:
            ipo = IpState(text)
            lastip = ipo.last_ip()
            self.line_ip.setText(lastip)
        except:
            pass
    
    # 0k
    @pyqtSlot()
    def on_btn_nextip_clicked(self):
        """
        Slot documentation goes here.
        """
        text = self.line_ip.text()
        try:
            ipo = IpState(text)
            nextip = ipo.next_ip()
            self.line_ip.setText(nextip)
        except:
            pass

    @pyqtSlot()
    def on_btn_starthandle_clicked(self):
        """
        Slot documentation goes here.
        """
        ipstr = self.line_ip.text()
        

        
    
    @pyqtSlot()
    def on_btn_continue_clicked(self):
        """
        Slot documentation goes here.
        """

    # 0k
    def set_background(self, row, back_color=QColor(29, 140, 29)):
        
        listcount = self.listWidget.count()
        if row > listcount:
            logger.debug("error, set curr row is out range rowcount, form QListWidget")
            return
        
        for _row in range(listcount):
            if _row == row:
                # 如果是找的IP, 项的背景颜色变成为兰色
                item = self.listWidget.item(_row)
                item.setBackground(back_color)
            else:
                # 如果不是找到的ip,项的背景颜色就恢复成原色
                item = self.listWidget.item(_row)
                item.setBackground(QColor(255, 255, 255))


def main():
    import sys
    from PyQt5.QtWidgets import QApplication
    app = QApplication(sys.argv)
    win = Dialog()
    win.show()
    app.exec_()




    
